﻿#pragma execution_character_set("utf-8")
#include "deliverywidget.h"
#include "ui_deliverywidget.h"
#include <QImage>
#include <QPixmap>
#include <QPainter>
#include "database.h"
#include <QApplication>

DeliveryWidget::DeliveryWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DeliveryWidget)
{
    ui->setupUi(this);
    exepath = QApplication::applicationDirPath();
    QStringList header;
    header <<"商品编号"<<"名称"<<"数量"<<"种类"<<"成本"<<"售价"<<"备注";
    ui->tableWidget->setColumnCount(7);
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    ui->tableWidget->horizontalHeader()->setFixedHeight(50);

    ui->tableWidget->verticalHeader()->setHidden(true);
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(50);
    updateDisplay();
    connect(ui->tableWidget, SIGNAL(cellClicked(int,int)),this,SLOT(onHeaderClicked(int,int)));

}

DeliveryWidget::~DeliveryWidget()
{
    delete ui;
}

void DeliveryWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QImage t_image;
    //    t_image.load(":/image/bg.png");
    //    t_image = t_image.scaled(this->width(),this->height());
    //    painter.drawPixmap(0,0,this->width(),this->height(),QPixmap::fromImage(t_image));

}

void DeliveryWidget::showEvent(QShowEvent *event)
{
    ui->tableWidget->horizontalHeader()->resizeSection(0,ui->tableWidget->width()*0.15);
    ui->tableWidget->horizontalHeader()->resizeSection(1,ui->tableWidget->width()*0.15);
    ui->tableWidget->horizontalHeader()->resizeSection(2,ui->tableWidget->width()*0.15);
    ui->tableWidget->horizontalHeader()->resizeSection(3,ui->tableWidget->width()*0.15);
    ui->tableWidget->horizontalHeader()->resizeSection(4,ui->tableWidget->width()*0.1);
    ui->tableWidget->horizontalHeader()->resizeSection(5,ui->tableWidget->width()*0.1);
    ui->tableWidget->horizontalHeader()->resizeSection(6,ui->tableWidget->width()*0.2);
    updateDisplay();
}

void DeliveryWidget::updateDisplay()
{
    ui->tableWidget->clearContents();
    QVector<QVector<QString>>  allinfo = DataBasePri::queryAll("products");
    ui->tableWidget->setRowCount(allinfo.size());
    for(int i =0;i < allinfo.size();i++){
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(allinfo.at(i).at(0)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(allinfo.at(i).at(1)));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(allinfo.at(i).at(2)));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(allinfo.at(i).at(3)));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(allinfo.at(i).at(4)));
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(allinfo.at(i).at(5)));
        ui->tableWidget->setItem(i,6,new QTableWidgetItem(allinfo.at(i).at(6)));
    }
}

void DeliveryWidget::onHeaderClicked(int row,int column)
{
    selectrow = row;
    ui->lineEdit_a->setText(ui->tableWidget->item(row,0)->text());
    ui->lineEdit_b->setText(ui->tableWidget->item(row,1)->text());
    ui->lineEdit_c->setText(ui->tableWidget->item(row,2)->text());
    ui->lineEdit_d->setText(ui->tableWidget->item(row,3)->text());
    ui->lineEdit_e->setText(ui->tableWidget->item(row,4)->text());
    ui->lineEdit_f->setText(ui->tableWidget->item(row,5)->text());
    ui->lineEdit_g->setText(ui->tableWidget->item(row,6)->text());

    QString imgstr = exepath+"/image/"+ui->tableWidget->item(row,0)->text()+".png";
    QImage t_image;
    t_image.load(imgstr);
    t_image = t_image.scaled(91,91);
    ui->label_dis->setPixmap(QPixmap::fromImage(t_image));
}

void DeliveryWidget::on_pushButton_a_clicked()
{

    QString lineEdit_a = ui->lineEdit_a->text();//获取界面输入
    QString lineEdit_b = ui->lineEdit_b->text();//获取界面输入
    QString lineEdit_c = ui->lineEdit_c->text();//获取界面输入
    QString lineEdit_d = ui->lineEdit_d->text();//
    QString lineEdit_e = ui->lineEdit_e->text();//
    QString lineEdit_f = ui->lineEdit_f->text();//
    QString lineEdit_g = ui->lineEdit_g->text();//

    if(lineEdit_a == "" || lineEdit_b == ""|| lineEdit_c == ""|| lineEdit_d == ""|| lineEdit_e == ""|| lineEdit_f == ""|| lineEdit_g == ""){//判断输入情况
        QMessageBox msgBox;
        msgBox.setWindowTitle("提示");
        msgBox.setText("输入不能为空");
        msgBox.exec();
    }else{
        if (! DataBasePri::database.open()){//打开数据库
            qCritical() << DataBasePri::database.lastError().text();
            return;
        }
        int t_num = ui->tableWidget->item(selectrow,2)->text().toInt() - lineEdit_c.toInt();
        if(t_num < 0){
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("出库数量超过库存！出库失败");
            msgBox.exec();
            return;
        }
        QSqlQuery query(DataBasePri::database);
        bool ret = 0;
        if(t_num == 0){
            ret =  query.exec(QString::fromLocal8Bit("delete from products where id = '%1'").arg(lineEdit_a));
        }else{
            ret =  query.exec(QString::fromLocal8Bit("update products set num = '%1' where id = '%2'").arg(t_num).arg(lineEdit_a));
        }
        if(ret == true){
            QString ctime = QDateTime::currentDateTime().toString("yyyy-MM-dd-hh-mm-ss");
            //出库记录
            QString t_str = QString::fromLocal8Bit("insert into delivery values('%1','%2','%3','%4','%5','%6','%7','无')")
                    .arg(ctime).arg(lineEdit_a).arg(lineEdit_b).arg(lineEdit_c).arg(lineEdit_d).arg(lineEdit_e).arg(lineEdit_f).arg(lineEdit_g);
            query.exec(t_str);
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("出库成功");
            msgBox.exec();

        }else{
            QMessageBox msgBox;
            msgBox.setWindowTitle("提示");
            msgBox.setText("出库失败");
            msgBox.exec();
        }
        DataBasePri::database.close();
        updateDisplay();
    }
}

void DeliveryWidget::on_pushButton_b_clicked()
{
    emit sendDisWidget("m_SelectWidget");
}
